package com.ihr360.payroll.service.salarytasklet.step.writer;


import com.ihr360.commons.data.util.Pair;
import com.ihr360.commons.lang.CollectionUtils;
import com.ihr360.job.core.item.ItemWriter;
import com.ihr360.payroll.calc.StaffSalaryCalcPeriod;
import com.ihr360.payroll.dao.PayPeriodTermRepository;
import com.ihr360.payroll.dao.StaffPayPeriodTermCalcContextRepository;
import com.ihr360.payroll.dao.StaffPayPeriodTermSalaryRepository;
import com.ihr360.payroll.model.dto.salary.embeddable.RefServiceStatistics;
import com.ihr360.payroll.model.dto.salary.task.period.PayPeriodTerm;
import com.ihr360.payroll.model.dto.salary.task.period.StaffPayPeriodTermCalcContext;
import com.ihr360.payroll.model.dto.salary.task.period.StaffPayPeriodTermSalary;
import com.ihr360.payroll.service.salarytasklet.convertor.StaffPayPeriodSalaryConvertor;
import com.ihr360.payroll.service.salarytasklet.model.PreSalaryCalcProcessedResult;
import com.ihr360.validation.ValidationUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

/**
 * @author davidwei
 */
@Component
public class StaffSalaryCalcContextWriter implements ItemWriter<PreSalaryCalcProcessedResult> {
    @Autowired
    private StaffPayPeriodTermCalcContextRepository staffPayPeriodTermCalcContextRepository;

    @Autowired
    private PayPeriodTermRepository payPeriodTermRepository;

    @Autowired
    private StaffPayPeriodSalaryConvertor convertor;

    @Autowired
    private StaffPayPeriodTermSalaryRepository staffPayPeriodTermSalaryRepository;

    @Override
    public void write(List<? extends PreSalaryCalcProcessedResult> result) throws Exception {
        Optional<? extends PreSalaryCalcProcessedResult> optional = result.stream().findFirst();
        if (!optional.isPresent()) {
            return;
        }
        PreSalaryCalcProcessedResult processedResult = optional.get();
        List<Pair<StaffPayPeriodTermCalcContext, StaffSalaryCalcPeriod>> salaryCalcContext = processedResult.getStaffContext();

        if (CollectionUtils.isNotEmpty(salaryCalcContext)) {
            List<StaffPayPeriodTermCalcContext> contextList = new ArrayList<>(salaryCalcContext.size());
            List<StaffPayPeriodTermSalary> salaryCalcPeriodList = new ArrayList<>(salaryCalcContext.size());
            for (Pair<StaffPayPeriodTermCalcContext, StaffSalaryCalcPeriod> pair : salaryCalcContext) {
                StaffPayPeriodTermCalcContext calcContext = pair.getFirst();
                contextList.add(calcContext);
                StaffPayPeriodTermSalary termSalary = convertor.convertEntityToDto(pair.getSecond());
                termSalary.setCompanyId(calcContext.getCompanyId());
                salaryCalcPeriodList.add(termSalary);
            }
            if (CollectionUtils.isNotEmpty(contextList)) {
                staffPayPeriodTermCalcContextRepository.save(contextList);
            }
            if (CollectionUtils.isNotEmpty(salaryCalcPeriodList)) {
                staffPayPeriodTermSalaryRepository.save(salaryCalcPeriodList);
            }
        }
        Long termId = processedResult.getTermId();
        ValidationUtils.rejectIfEmpty(termId, null, "未指定账期标识");
        RefServiceStatistics refServiceStatistics = processedResult.getRefServiceStatistics();
        PayPeriodTerm periodTerm = payPeriodTermRepository.findOne(termId);
        ValidationUtils.rejectIfEmpty(periodTerm, null, "无法获取账期信息");
        periodTerm.setRefServiceStatistics(refServiceStatistics);
        payPeriodTermRepository.save(periodTerm);
    }
}